/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The impact of divorce laws on the equilibrium in the marriage market.
% Ana Reynoso
% April 2024
%
% This file inputs simulations from the model and produces the values in the
% Model column in Table A6.
%  
% Data: PSID 1968-1992
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

*%------------------------ Replication Path -------------------------------%

clear all

*%--- Indicate location of Replication folder:
*global replication_location "C:\update_with_your_path"

*%------------------------ Preliminaries ----------------------------------%

set seed 123456789

cd "$replication_location\Validation\Impact_already_married\Inputs"

qui{
	
*%------------------------ Simulated data ----------------------------------%

forvalues i=1(1)9{
insheet using decg`i'.cvs, clear

gen n=_n
gen udt=1 
replace udt=2 if _n>10000 & _n<=20000
replace udt=3 if _n>20000 & _n<=30000
replace udt=4 if _n>30000 & _n<=40000
replace udt=5 if _n>40000 & _n<=50000
replace udt=6 if _n>50000 & _n<=60000
replace udt=7 if _n>60000 & _n<=70000
replace udt=8 if _n>70000 & _n<=80000
replace udt=9 if _n>80000 & _n<=90000
replace udt=10 if _n>90000 & _n<=100000

bysort udt: replace n=_n

gen freq_g1=50.26/100
gen freq_g2=12.81/100
gen freq_g3=2.29/100
gen freq_g4=10.66/100
gen freq_g5=10.81/100
gen freq_g6=3.91/100
gen freq_g7=1.31/100
gen freq_g8=2.49/100
gen freq_g9=5.46/100

save decg`i'.dta, replace

}

forvalues i=1(1)9{
use decg`i'.dta, clear

drop if n> freq_g`i'*10000 						   

save decg`i'.dta, replace
}

use decg1, clear

sample 71.93 if udt==1
sample 1.64 if udt==2
sample 1.75 if udt==3
sample 1.61 if udt==4
sample 1.68 if udt==5
sample 2 if udt==6
sample 1.86 if udt==7
sample 1.97 if udt==8
sample 1.64 if udt==9
sample 10.62 if udt==10

save decg1.dta, replace

use decg2, clear

sample 62.54 if udt==1
sample 4.13 if udt==2
sample 3.06 if udt==3
sample 2.75 if udt==4
sample 1.99 if udt==5
sample 1.99 if udt==6
sample 2.14 if udt==7
sample 2.75 if udt==8
sample 2.14 if udt==9
sample 11.47 if udt==10

save decg2.dta, replace

use decg3, clear

sample 58.82 if udt==1
sample 3.36 if udt==2
sample 1.68 if udt==3
sample 5.04 if udt==4
sample 3.36 if udt==5
sample 2.52 if udt==6
sample 2.52 if udt==7
sample 3.36 if udt==8
sample 4.20 if udt==9
sample 14.29 if udt==10

save decg3.dta, replace

use decg4, clear

sample 72.43 if udt==1
sample 2.44 if udt==2
sample 2.62 if udt==3
sample 2.62 if udt==4
sample 1.75 if udt==5
sample 1.40 if udt==6
sample 1.57 if udt==7
sample 1.40 if udt==8
sample 1.05 if udt==9
sample 7.68 if udt==10

save decg4.dta, replace

use decg5, clear

sample 64.03 if udt==1
sample 5.27 if udt==2
sample 3.77 if udt==3
sample 3.58 if udt==4
sample 2.45 if udt==5
sample 2.07 if udt==6
sample 1.88 if udt==7
sample 2.07 if udt==8
sample 2.64 if udt==9
sample 7.53 if udt==10

save decg5.dta, replace

use decg6, clear

sample 59.68 if udt==1
sample 2.15 if udt==2
sample 5.38 if udt==3
sample 2.69 if udt==4
sample 3.76 if udt==5
sample 2.69 if udt==6
sample 2.69 if udt==7
sample 3.23 if udt==8
sample 1.61 if udt==9
sample 11.83 if udt==10

save decg6.dta, replace

use decg7, clear

sample 73.24 if udt==1
sample 1.41 if udt==2
sample 1.41 if udt==3
sample 1.41 if udt==4
sample 2.82 if udt==5
drop if udt==6
sample 1.41 if udt==7
sample 1.41 if udt==8
drop if udt==9
sample 15.49 if udt==10

save decg7.dta, replace

use decg8, clear

sample 68.64 if udt==1
sample 4.24 if udt==2
sample 2.54 if udt==3
sample 5.93 if udt==4
sample 3.39 if udt==5
drop if udt==6
sample 2.54 if udt==7
sample 0.85 if udt==8
sample 1.69 if udt==9
sample 2.54 if udt==10

save decg8.dta, replace

use decg9, clear

sample 62.82 if udt==1
sample 5.13 if udt==2
sample 2.56 if udt==3
sample 1.28 if udt==4
sample 2.99 if udt==5
sample 3.42 if udt==6
sample 4.70 if udt==7
sample 3.42 if udt==8
sample 1.28 if udt==9
sample 8.55 if udt==10

save decg9.dta, replace
 
* Reshape to have a panel

forvalues i=1(1)9{

use decg`i'.dta, clear
sort udt n
gen couple=_n

reshape long v, i(couple) j(period)
gen type=`i'
keep couple period v udt type

gen married=(v!=-1)
gen wife_inlf=(v==1)
gen divorce=(v==3)
replace udt=0 if udt==1
gen ud=0
replace ud= 1 if period >=udt 
replace ud=0 if udt==0

save dec`i'_rshpd, replace

}

* Append all data together
 
use dec1_rshpd.dta, clear

forvalues i=2(1)9{
append using dec`i'_rshpd

}

egen hh=group(type couple)
tab period, gen(age_int)
tab type, gen(group)

bysort hh: egen dur=sum(married)

cd "$replication_location\Validation\Impact_already_married\Output"
}

* Build column Model in Table A6

tempname ests
	mat `ests'=J(3,1,.)

qui: xtreg wife_inlf ud age_int* if married==1 & divorce==0 & udt!=0, fe i(hh) nonest 
mat `ests'[1,1]=round(_b[ud],.001)
qui: reg wife_inlf ud age_int* if married==1 & divorce==0 & udt!=0 & period<=4, robust 
mat `ests'[2,1]=round(_b[ud],.001)
qui: gen divorced=(v==3 | v==-1)
qui: xtreg divorced ud age_int* , fe i(hh) nonest 
qui: sum divorced
mat `ests'[3,1]=round(_b[ud]/r(mean)*100 ,.1)

log using Table_A6_Model.log, replace

* Table A6-Column Model:

di "Employment of married women: " `ests'[1,1]

di "Employment of newlywed women: " `ests'[2,1]

di "Divorced: " `ests'[3,1]

log close









